package com.uduew.sjd.ui.mine

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.uduew.sjd.BR
import com.uduew.sjd.MainActivity
import com.uduew.sjd.R
import com.uduew.sjd.config.AppConfig
import com.uduew.sjd.message.Messages
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.Utils
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State

class MineFragment : BaseFragment() {

    lateinit var mState: MineStates
    private lateinit var mMessenger: PageMessenger

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(MineStates::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)
    }


    private fun setData() {
        mState.loginState.set(AppConfig.isLogin)
        AppConfig.mConfigs.phone().get().let {
            if (it != null) {
                val phone = if (it.length > 8) {
                    it.substring(0, 3) + "XXX" + it.substring(it.length - 3)
                } else {
                    // 不足8位显示原值
                    it
                }
                mState.userPhone_tv.set("+84 $phone")
            }
        }
    }


    override fun onResume() {
        super.onResume()
        setData()
        mState.unfinishedFormCount.set(AppConfig.unfinishedFormCount)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setData()
        mMessenger.output(this) { messages ->
            if (messages.eventId == Messages.EVENT_LOGIN_SUCCESS) {
                setData()
            }
            if (messages.eventId == Messages.EVENT_CERTIFICATION_SUCCESS) {
                mState.unfinishedFormCount.set(0)
            }
            if (messages.eventId == Messages.EVENT_LOGIN_OUT_SUCCESS) {
                startActivity(Intent(context, MainActivity::class.java))
                requireActivity().finish()
            }

        }
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.fragment_mine, BR.vm, mState).addBindingParam(
            BR.click,
            Click()
        )
    }


    inner class Click {

        fun allLoanOrders() {
            if (!Utils.checkDebounce()) return
            nav().navigate(R.id.action_navigation_main_to_all_loan_orders_fragment)
        }

        //Go to log in
        fun goLion() {

        }

        //Pending approval
        fun pendingApproval() {
            if (!Utils.checkDebounce()) return
            val bundle = Bundle()
            bundle.putInt("type", 1)
            nav().navigate(R.id.action_navigation_main_to_all_loan_orders_fragment, bundle)

        }

        fun agreement() {
            if (!Utils.checkDebounce()) return
            nav().navigate(R.id.action_navigation_main_to_privacy_fragment)
        }

        //Pending payment
        fun setting() {
            if (!Utils.checkDebounce()) return
            nav().navigate(R.id.action_navigation_main_to_setting_fragment)

        }

        //Pending payment
        fun aboutUs() {
            if (!Utils.checkDebounce()) return
            nav().navigate(R.id.action_navigation_main_to_about_us_fragment)
        }

        //Pending payment 待还款 firemen
        fun pendingPayment() {
            if (!Utils.checkDebounce()) return
            val bundle = Bundle()
            bundle.putInt("type", 2)
            nav().navigate(R.id.action_navigation_main_to_all_loan_orders_fragment, bundle)
        }

        //Settled  已结清 missive
        fun settled() {
            if (!Utils.checkDebounce()) return
            val bundle = Bundle()
            bundle.putInt("type", 8)
            nav().navigate(R.id.action_navigation_main_to_all_loan_orders_fragment, bundle)
        }

        fun bankList() {

            if (!Utils.checkDebounce()) return
            val bundle = Bundle()
            bundle.putBoolean("isLoan", false)
            nav().navigate(R.id.action_navigation_main_to_bank_list_fragment,bundle)
        }


        fun feedBack() {
            if (!Utils.checkDebounce()) return
            nav().navigate(R.id.action_navigation_main_to_feed_back_fragment)
        }
    }

    class MineStates : StateHolder() {
        val userPhone_tv: State<String> = State("未登入")
        val usernick_tv: State<String> = State("立即登入，開啟美好生活")
        val avatar: State<String> = State("")
        val unfinishedFormCount: State<Int> = State(AppConfig.unfinishedFormCount)
        val loginState: State<Boolean> = State(false)
    }

}